package com.mxy.myapplication.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mxy.myapplication.R;
import com.mxy.myapplication.activity.MainActivity;
import com.mxy.myapplication.adapter.AdapterForRv_Linear_ItemTouchHelper;
import com.mxy.myapplication.pojo.Pojo1;
import com.mxy.myapplication.util.LinearLayoutColorDivider;
import com.mxy.myapplication.util.itemtouchhelperUtil.SimpleItemTouchHelperCallback;

import java.util.ArrayList;

/**
 * ViewPager上的碎片 线性布局
 */

public class FragmentOfViewPager_Linear_ItemTouchHelper extends Fragment {

    ArrayList<Pojo1> data = new ArrayList<Pojo1>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_linearlayout_itemtouchhelper, container, false);
        RecyclerView rv = (RecyclerView) v.findViewById(R.id.rv);

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.VERTICAL);

        rv.setLayoutManager(llm);
        rv.setItemAnimator(new DefaultItemAnimator());
        rv.addItemDecoration(new LinearLayoutColorDivider(getActivity().getResources(),
                R.color.EFEFEF, R.dimen.dip1, LinearLayoutManager.VERTICAL));

        initData();
        MainActivity mainActivity = (MainActivity) this.getActivity();
        AdapterForRv_Linear_ItemTouchHelper adapter =
                new AdapterForRv_Linear_ItemTouchHelper(mainActivity, data, mainActivity.swipe);
        SimpleItemTouchHelperCallback simpleItemTouchHelperCallback =
                new SimpleItemTouchHelperCallback(adapter);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchHelperCallback);
        itemTouchHelper.attachToRecyclerView(rv);
        rv.setAdapter(adapter);

        return v;
    }

    public void initData() {
        data.clear();
        data.add(new Pojo1(1, R.drawable.jj1, "进击的巨人"));
        data.add(new Pojo1(0, R.drawable.jj2, "进击的巨人"));
        data.add(new Pojo1(0, R.drawable.jj3, "进击的巨人"));
        data.add(new Pojo1(1, R.drawable.bleach1, "死神"));
        data.add(new Pojo1(0, R.drawable.bleach2, "死神"));
        data.add(new Pojo1(0, R.drawable.bleach3, "死神"));
        data.add(new Pojo1(1, R.drawable.guanlan1, "灌篮高手"));
        data.add(new Pojo1(0, R.drawable.guanlan2, "灌篮高手"));
        data.add(new Pojo1(0, R.drawable.guanlan3, "灌篮高手"));
    }
}